package com.fruit.controller.wap.packageorder;
  
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.orderpackage.PackageOrderInfo;
import com.fruit.service.packageorder.PackageOrderManager;
import com.fruit.util.CommonUtil;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 产品套餐订单相关
 * 
 * @author Administrator
 * @version 2018.02.02 12:40
 */
@Slf4j
@Controller
@RequestMapping(value = "/wap/packageorder")
public class PackageOrderWapController extends BaseController {
	@Autowired
	private PackageOrderManager packageOrdeService;
	
	/**
	 * 确认订单  <br/>
	 * 
	 * @param id
	 *          产品套餐id
	 * @param buyNum
	 *          购买数量
	 * @param request
	 *            请求
	 * @param session
	 *            session
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "/firmOrder", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> firmOrder(@RequestParam("id") Long id, @RequestParam("buyNum") Integer buyNum,
			HttpServletRequest request, HttpSession session) {
		String tradeId = CommonUtil.getUUID();
		log.info("【" + tradeId + "】---【确认订单】---【开始】--");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if (id == null) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数id为空!"));
				return result;
			}
			if (buyNum == null || buyNum <= 0) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数购买数量必须大于零!"));
				return result;
			}
			log.info("【" + tradeId + "】---【确认订单】---【params】--id=" + id + "buyNum=" + buyNum);
			Integer userId = (Integer) session.getAttribute("userId");
			
			Map<String, Object> pam = new HashMap<String, Object>();
			pam.put("goodsPackageId", id);
			pam.put("userId", userId);
			pam.put("buyNum", buyNum);
			result = packageOrdeService.findCheckFirmOrderParams(pam, tradeId);
			log.info("【" + tradeId + "】---【确认订单】---【操作成功】--");
		} catch (Exception e) {
			log.error("【" + tradeId + "】---【确认订单】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【" + tradeId + "】---【确认订单】---【结束】--{}", result.toString());
		return result;
	}
	
	/**
	 * 订单提交  <br/>
	 * 
	 * @param pi
	 *          订单信息
	 * @param request
	 *            请求
	 * @param session
	 *            session
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "/submitOrder", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> submitOrder(PackageOrderInfo pi,	HttpServletRequest request, HttpSession session) {
		String tradeId = CommonUtil.getUUID();
		log.info("【" + tradeId + "】---【订单提交】---【开始】--");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if (pi == null) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数为空!"));
				return result;
			}
			if (pi.getGoodsPackageId() == null) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数套餐id为空!"));
				return result;
			}
			if (pi.getBuyNum() <= 0) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数购买数量必须大于零!"));
				return result;
			}
			log.info("【" + tradeId + "】---【订单提交】---【params】--{}", pi.toString());
			Integer userId = (Integer) session.getAttribute("userId");
			
			Map<String, Object> pam = new HashMap<String, Object>();
			pam.put("goodsPackageId", pi.getGoodsPackageId());
			pam.put("userId", userId);
			pam.put("buyNum", pi.getBuyNum());
			pam.put("submitToken", pi.getSubmitToken());
			pam.put("addressId", pi.getAddressId());
			pam.put("totalPrice", pi.getTotalPrice());
			
			result = packageOrdeService.saveSubmitOrder(pam, tradeId);
		} catch (Exception e) {
			log.error("【" + tradeId + "】---【订单提交】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【" + tradeId + "】---【订单提交】---【结束】--");
		return result;
	}
	
	/**
	 * 订单套餐权益详情
	 * 
	 * @param orderId
	 *          订单id
	 * @param goodsPackageId
	 *          产品套餐id
	 * @param request
	 *            请求
	 * @param session
	 *            session
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "/orderRight", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getOrderRightRecords(@RequestParam("orderId") Long orderId, @RequestParam("goodsPackageId") Long goodsPackageId, 
			HttpServletRequest request, HttpSession session) {
		log.info("【订单套餐权益详情】---【开始】--");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if (orderId == null) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数订单标识为空!"));
				return result;
			}
			if (goodsPackageId == null) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数套餐标识为空!"));
				return result;
			}
			log.info("【订单套餐权益详情】---【params】--orderId=" + orderId + ",goodsPackageId=" + goodsPackageId);
			Integer userId = (Integer) session.getAttribute("userId");
			
			Map<String, Object> pam = new HashMap<String, Object>();
			pam.put("userId", userId);
			pam.put("orderId", orderId);
			pam.put("goodsPackageId", goodsPackageId);
			result = packageOrdeService.getOrderRightRecords(pam);
			
			log.info("【订单套餐权益详情】---【操作成功】--");
		} catch (Exception e) {
			log.error("【订单套餐权益详情】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【订单套餐权益详情】---【结束】--");
		return result;
	}
	/**
	 * 实名认证<br/>
	 * @param goodsPackageId
	 *          产品套餐id
	 * @param request
	 *            请求
	 * @param session
	 *            session
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "/isRealName", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> isRealName(@RequestParam("goodsPackageId") Long goodsPackageId, 
			HttpServletRequest request, HttpSession session) {
		log.info("【实名认证】---【开始】--");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Integer userId = (Integer) session.getAttribute("userId");
			log.info("【实名认证】---【用户标识】--{}", userId);
			if (userId == null) {
				log.error("【实名认证】---【获取用户标识失败】");
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "获取用户登录异常!"));
				return result;
			}
			if (goodsPackageId == null) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数套餐标识为空!"));
				return result;
			}
			Map<String, Object> pam = new HashMap<String, Object>();
			pam.put("userId", userId);
			pam.put("goodsPackageId", goodsPackageId);
			
			String tradeId = CommonUtil.getUUID();
			result = packageOrdeService.isRealName(pam, tradeId);
			log.info("【实名认证】---【操作成功】--【用户标识】--{}", result.toString());
		} catch (Exception e) {
			log.error("【实名认证】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【实名认证】---【结束】--");
		return result;
	}
}
